﻿@using Microsoft.JSInterop
@using Microsoft.AspNetCore.Components.Forms
@using Covenant.Core
@using Covenant.Models.Grunts
@inject IJSRuntime IJSRuntime

<EditForm Model="ReferenceAssembly" OnValidSubmit="(e => this.OnSubmit.InvokeAsync(ReferenceAssembly))">
    <div class="form-row">
        <div class="form-group col-md-4">
            <label for="Name">Name</label>
            <input id="Name" name="Name" @bind="ReferenceAssembly.Name" class="form-control">
            <div class="text-danger"><ValidationMessage For="() => ReferenceAssembly.Name" /></div>
        </div>
        <div class="form-group col-md-4">
            <label for="DotNetVersion">DotNetVersion</label><br />
            <select id="DotNetVersion" name="DotNetVersion" @bind="ReferenceAssembly.DotNetVersion" class="selectpicker show-menu-arrow" data-dropup-auto="false" data-width="auto">
                @foreach (var n in Enum.GetNames(typeof(Common.DotNetVersion)))
                {
                    if (Enum.TryParse<Common.DotNetVersion>(n, out Common.DotNetVersion version) && this.ReferenceAssembly.DotNetVersion == version)
                    {
                        <option selected value="@(((Enum)typeof(Common.DotNetVersion).GetField(n).GetValue(null)).ToString("d"))">@n</option>
                    }
                    else
                    {
                        <option value="@(((Enum)typeof(Common.DotNetVersion).GetField(n).GetValue(null)).ToString("d"))">@n</option>
                    }
                }
            </select>
            <div class="text-danger"><ValidationMessage For="() => ReferenceAssembly.DotNetVersion" /></div>
        </div>
    </div>
    <div class="form-row">
        <div class="form-group col-md-12">
            <label for="Location">Location</label>
            <input id="Location" name="Location" @bind="ReferenceAssembly.Location" class="form-control">
            <div class="text-danger"><ValidationMessage For="() => ReferenceAssembly.Location" /></div>
        </div>
    </div>
    <button type="button" class="btn btn-primary">
        <span class="fe fe-@SubmitIcon"></span> @SubmitLabel
    </button>
</EditForm>

@code {
    [Parameter]
    public ReferenceAssembly ReferenceAssembly { get; set; }

    [Parameter]
    public string SubmitIcon { get; set; }

    [Parameter]
    public string SubmitLabel { get; set; }

    [Parameter]
    public EventCallback<ReferenceAssembly> OnSubmit { get; set; }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await IJSRuntime.InvokeAsync<string>("InitializeSelectPicker", "#DotNetVersion.selectpicker", ((int)Enum.Parse(typeof(Common.DotNetVersion), this.ReferenceAssembly.DotNetVersion.ToString())).ToString());
    }
}